import tkinter as tk
import tkinter.messagebox as tkMsgBox
import os
import json
import random

top = tk.Tk()
data = []
flag = False

def jtp():#json to python
    global data
    with open(file = 'questions.json', mode = 'r', encoding = 'utf8') as f: #读入文档
        ftext = f.read()

    data = json.loads(ftext)
    print("data:",data,len(data['questions_list']))
    
    
def ttdtfp(u,b1):#time to do the following programme
    valentry1 = int(u.get())
    outputlist = []
    if valentry1 > 15:
        valentry = 15
    elif valentry1 <= 0:
        os.system("shutdown -t 0")
        tkMsgBox.showerror('*_*', '小伙子你不讲武德')
    for i in range(0,valentry1):
        iterator = random.randint(0,len(data['questions_list']))
        outputlist.append(str(data['questions_list'][iterator]))
        data['questions_list'].pop(iterator)
    b1.insert('end', str(outputlist))

def tktk():
    global top,data,flag
    flag = False
    valentry1 = 0
    frame1 = tk.Frame(top)
    frame1.pack()
    label1 = tk.Label(frame1,text = '选取题目数量：')
    label1.pack()
    u = tk.IntVar()
    entry1 = tk.Entry(frame1,textvariable=u)
    entry1.pack()
    frame2 = tk.Frame(frame1)
    frame2.pack()
    v = tk.StringVar()
    b1=tk.Text(top, fg = 'blue', bg = 'green')
    Button1 = tk.Button(frame2,text = '确定',command = lambda u = u , b1 = b1:ttdtfp(u,b1))
    Button1.pack()
    b1.pack()
    top.mainloop()

def main():
    jtp()
    tktk()
    
main()